using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Globals;
using Presentation.Contracts;
using Presentation;

namespace ProjektZespolowy.test
{
    public partial class Friends : Page, IFriendsView
    {
        #region Members

        /// <summary>
        /// Instance of presenter.
        /// </summary>
        private FriendsPresenter presenter;

        #endregion Members

        #region Override Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new FriendsPresenter(this);
        }

        #endregion Override Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                    b1.Visible = false;
                    b2.Visible = false;
                    b3.Visible = false;
                    b4.Visible = false;
                    b5.Visible = false;
                    b6.Visible = false;

                    int numberOfPages = presenter.FindNrOfPages();
                    Session["nrOfPages"] = numberOfPages;

                    if (numberOfPages <= 6)
                    {
                        this.InitPage1(numberOfPages);  
                        presenter.FindFriendsPerPage(1);
                      
                    }
                    else
                    {
                        this.InitPage2();
                        presenter.FindFriendsPerPage(1);
                    }

                    presenter.SetCaption();
                }


          }


        public void InitPage1(int numberOfPages)
        {
            int i_tmp = 0;

                  for (int i = 1; i <= numberOfPages; i++)
                        {
                            for (int j = 1; j <= i; j++)
                            {
                                if (j > 1)
                                    i_tmp--;
                                else
                                    i_tmp = i;

                                Button b_tmp = (Button)UpdatePanel1.FindControl("b" + j.ToString());
                                b_tmp.Text = i_tmp.ToString();
                                b_tmp.Visible = true;

                                if (i == numberOfPages && j == i)
                                {
                                    b_tmp = (Button)UpdatePanel1.FindControl("b" + j.ToString());
                                    b_tmp.Enabled = false;
                                }

                            }
                        }
        }

        public void InitPage2()
        {
            b1.Text = "->";
            b1.CommandName = "Next";
            b1.Visible = true;
            b2.Text = "5";
            b2.Visible = true;
            b3.Text = "4";
            b3.Visible = true;
            b4.Text = "3";
            b4.Visible = true;
            b5.Text = "2";
            b5.Visible = true;
            b6.Text = "1";
            b6.Enabled = false;
            b6.Visible = true;
        }
  
        public string FormatURL(object _id)
   {

       int num = Convert.ToInt32(_id);

       return ("~/GetUserPicture.aspx?id=" + _id);

   }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                 e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hp = (HyperLink)e.Item.FindControl("HyperLink1");
                hp.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "login"));
                
                ImageButton img = (ImageButton)e.Item.FindControl("IAvatarFriend");

                img.Height = 80;
                img.Width = 80;
                img.ImageUrl = FormatURL(DataBinder.Eval(e.Item.DataItem,"id"));
                hp.NavigateUrl = "Friends.aspx?id=" + DataBinder.Eval(e.Item.DataItem,"id").ToString();
                img.CommandName = DataBinder.Eval(e.Item.DataItem,"id").ToString();

                ImageButton imgButtonM = (ImageButton)e.Item.FindControl("bSendMessage");
                ImageButton imgButtonA = (ImageButton)e.Item.FindControl("bAddFriend");
                ImageButton imgButtonD = (ImageButton)e.Item.FindControl("bDeleteFriend");
                   
                imgButtonM.ImageUrl = "/images/msg.gif";
                imgButtonA.ImageUrl = "/images/ptaszek.gif";
                imgButtonD.ImageUrl = "/images/delete.gif";
                imgButtonA.Visible=true;
                imgButtonD.Visible=true;
                imgButtonM.Visible=true;
                imgButtonA.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();
                imgButtonD.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();
                imgButtonD.CommandArgument = DataBinder.Eval(e.Item.DataItem, "login").ToString();
                imgButtonM.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();

                if (StrID().CompareTo( Session[ContentNamesGlobals.SessionWithUserId].ToString())==0)
                {
                    imgButtonA.Visible = false;
                }
                else
                {
                    if(presenter.FindCommonFriends(int.Parse(StrID()),int.Parse( Session[ContentNamesGlobals.SessionWithUserId].ToString()),int.Parse(DataBinder.Eval(e.Item.DataItem, "id").ToString()))){
                    
                            imgButtonA.Visible = false;
                            imgButtonD.Visible = false;

                        }
                        else
                        {
                            imgButtonA.Visible = true;
                            imgButtonD.Visible = false;

                        }
                    

                    if(DataBinder.Eval(e.Item.DataItem, "id").ToString().CompareTo(Session[ContentNamesGlobals.SessionWithUserId].ToString())==0)
                    {
                    imgButtonM.Visible=false;
                    imgButtonA.Visible = false;
                    imgButtonD.Visible = false;
                }
                }
                
            }

        }

        protected void IAvatarFriend_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;
            
            Response.Redirect("/Friends/Friends.aspx?id="+img.CommandName);
        }


        protected void b6_Click(object sender, EventArgs e)
        {
            if (b6.CommandName == "Prev")
            {
                int nr_str=0;
                int idLastButton = 0;
                for (int i = 2; i <= 5; i++)
                {
                    Button b_disp = (Button)UpdatePanel1.FindControl("b" + i.ToString());

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 5)
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_disp = (Button)UpdatePanel1.FindControl("b" + i.ToString());

                        b_disp.Text = ((int)(nr_str - (i-1))).ToString();

                        b_disp.Visible = true;
                        b_disp.Enabled = true;

                    }

                    b1.Visible = true;
                    
                    if (int.Parse(b5.Text) == 2)
                    {
                        b6.Text = "1";
                        b6.CommandName = "";
                    }

                    b2.Enabled = false;
             
                   
                    presenter.FindFriendsPerPage(int.Parse(b2.Text));

                }
                else
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_temp = (Button)UpdatePanel1.FindControl("b" + i.ToString());
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel1.FindControl("b" + (i + 1).ToString());
                            b_temp.Enabled = false;

                            presenter.FindFriendsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b6.Enabled = false;
                b1.Enabled = true;
                b2.Enabled = true;
                b3.Enabled = true;
                b4.Enabled = true;
                b5.Enabled = true;
            
                presenter.FindFriendsPerPage(int.Parse(b6.Text));
            }
          
        }

        protected void b5_Click(object sender, EventArgs e)
        {
            b1.Enabled = true;
            b2.Enabled = true;
            b3.Enabled = true;
            b4.Enabled = true;
            b5.Enabled = false;
            b6.Enabled = true;
        
            presenter.FindFriendsPerPage(int.Parse(b5.Text));

        }

        protected void b4_Click(object sender, EventArgs e)
        {
            b1.Enabled = true;
            b2.Enabled = true;
            b3.Enabled = true;
            b4.Enabled = false;
            b5.Enabled = true;
            b6.Enabled = true;
        
            presenter.FindFriendsPerPage(int.Parse(b4.Text));

        }

        protected void b3_Click(object sender, EventArgs e)
        {
            b1.Enabled = true;
            b2.Enabled = true;
            b3.Enabled = false;
            b4.Enabled = true;
            b5.Enabled = true;
            b6.Enabled = true;
          
           
            presenter.FindFriendsPerPage(int.Parse(b3.Text));

        }

        protected void b2_Click(object sender, EventArgs e)
        {
            b1.Enabled = true;
            b2.Enabled = false;
            b3.Enabled = true;
            b4.Enabled = true;
            b5.Enabled = true;
            b6.Enabled = true;
          
            presenter.FindFriendsPerPage(int.Parse(b2.Text));

        }

        protected void b1_Click(object sender, EventArgs e)
        {
            if (b1.CommandName == "Next")
            {
                int nr_str;
                int idLastButton = 0;
                for (int i = 2; i <= 6; i++)
                {
                    Button b_disp = (Button)UpdatePanel1.FindControl("b" + i.ToString());

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 2)
                {
                    if (b6.Text.CompareTo("1") == 0)
                    {
                        b6.Text = "<-";
                        b6.CommandName = "Prev";
                    }
                    for (int i = 5; i >= 2; i--)
                    {
                        Button b_disp = (Button)UpdatePanel1.FindControl("b" + i.ToString());

                        if ((int.Parse(b_disp.Text) + 4) <= (int)Session["nrOfPages"])
                            b_disp.Text = ((int)(int.Parse(b_disp.Text) + 4)).ToString();
                        else
                        {
                            b_disp.Visible = false;
                            b1.Visible = false;

                        }
                        b_disp.Enabled = true;

                    }

                    if (int.Parse(b2.Text) == (int)Session["nrOfPages"])
                        b1.Visible = false;

                    b5.Enabled = false;
                 
                    presenter.FindFriendsPerPage(int.Parse(b5.Text));

                }
                else
                {
                    for (int i = 2; i <= 6; i++)
                    {
                        Button b_temp = (Button)UpdatePanel1.FindControl("b" + i.ToString());
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel1.FindControl("b" + (i - 1).ToString());
                            b_temp.Enabled = false;
                       
                            presenter.FindFriendsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b1.Enabled = false;
                b2.Enabled = true;
                b3.Enabled = true;
                b4.Enabled = true;
                b5.Enabled = true;
                b6.Enabled = true;

               presenter.FindFriendsPerPage(int.Parse(b1.Text));
            }
          

        }

        #region IFriendsView Members

        public void ShowFriends(DataTable dt,int v)
        {
            ViewState["tabela"] = dt;
            DataList1.DataSource = ViewState["tabela"];
            DataList1.DataBind();
        }

        public string StrID()
        {
            string strID;
            if (!string.IsNullOrEmpty(Request["id"]))
                strID = Request["id"];
            else
                strID = Session[ContentNamesGlobals.SessionWithUserId].ToString();
            return strID;
        }

        public string SessionID(){

            return Session[ContentNamesGlobals.SessionWithUserId].ToString();
    
    }

        public void SetHeader(string UserId)
        {
            Label lh = (Label)View1.FindControl("lHeader");

            if (UserId.CompareTo(Session[ContentNamesGlobals.SessionWithUserId].ToString()) == 0)
                lh.Text = "Moi Znajomi ("+presenter.GetNumberOfAllFriends().ToString()+")";
            else
                lh.Text = "Znajomi uzytkownika " + presenter.AskToLoginOfFriend(int.Parse(UserId))+" ("+presenter.GetNumberOfAllFriends().ToString()+")";
                
        }

        #endregion

        protected void bDeleteFriend_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;
           // View1.EnableViewState = false;
           // View2.EnableViewState = true;
            MultiView1.SetActiveView(View2);
            //View1.Visible = false;
            //View2.Visible = true;
           // string slog=presenter.AskToDeleteFriend(int.Parse(img.CommandName));
          
            lText.Text = "Czy na pewno chcesz usunac uzytkownika o loginie: " + img.CommandArgument + " z listy swoich znajomych?";
            bDelete.CommandName = img.CommandName;

            

        }

        protected void bSendMessage_Click(object sender,ImageClickEventArgs e)
        {
            ImageButton img=sender as ImageButton;
            Response.Redirect("/Mail/CVMessage.aspx?UserID="+img.CommandName);
        }

        protected void bAddFriend_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;
            
            //MultiView1.SetActiveView(View1);
            presenter.SendInvitation(int.Parse(img.CommandName));
            presenter.FindFriendsPerPage(1);
            //Response.Redirect("/Friends/Friends.aspx");

        }

        protected void bDelete_Click(object sender, EventArgs e)
        {
            presenter.DelFriend(int.Parse(bDelete.CommandName));
            lText.Text = "Usunieto znajomosc.";
            bDelete.Visible = false;
            bCancel.Visible = false;
            bOk.Visible = true;

        }

        protected void bCancel_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(View1);
            //View1.Visible = true;
            //View2.Visible = false;
        }

        protected void bOk_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(View1);
            //View1.Visible = true;
            //View2.Visible = false;
            //presenter.FindFriendsPerPage(1);
            //presenter.SetCaption();
            Response.Redirect("/Friends/Friends.aspx");
        }

        
     
    }

}